﻿using Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace RedisHelper.Article
{
    public class Like
    {
        /// <summary>
        /// 查询文章是否被当前用户点赞
        /// </summary>
        /// <param name="articleId"></param>
        /// <param name="uid"></param>
        /// <returns>true已点赞</returns>
        public bool IsArticleLike(int articleId, int uid)
        {
            using (RedisHelper rh = new RedisHelper())
            {
                var rdata = rh.GetDatabase(5).SetScan(StaticConfigRidesName.ArticleLike_Set(articleId));
                if (rdata.FirstOrDefault(u => u == uid).IsNull)
                    return false;
                return true;
            }
        }

        /// <summary>
        /// 获取文章点赞数
        /// </summary>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public long GetArticleLikeCount(int articleId)
        {
            using (RedisHelper rh = new RedisHelper())
            {
                return rh.GetDatabase(5).SetLength(StaticConfigRidesName.ArticleLike_Set(articleId));
            }
        }

        /// <summary>
        /// 文章点赞
        /// </summary>
        /// <returns></returns>
        public bool InsertArticleLike(int articleId, int uid)
        {
            using (RedisHelper rh = new RedisHelper()) {
                return rh.GetDatabase(5).SetAdd(StaticConfigRidesName.ArticleLike_Set(articleId), uid);
            }
        }

        /// <summary>
        /// 取消文章点赞
        /// </summary>
        /// <returns></returns>
        public bool DelArticleLike(int articleId, int uid)
        {
            using (RedisHelper rh = new RedisHelper())
            {
                return rh.GetDatabase(5).SetRemove(StaticConfigRidesName.ArticleLike_Set(articleId), uid);
            }
        }
    }
}
